home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / go4gw1.1 / go4gw.sun < prev    next >
Encoding:
Text File  |  1993-04-30  |  2.6 KB  |  116 lines

  1. #!/usr/local/bin/perl
  2. #----------------------------------------------------------------------
  3. # variables to change
  4.  
  5. $Gconf_file = "/home/ashpool/go4gw/go4gw.conf"; # configuration file
  6. $Gport=4324;                               # port THIS daemon is running on
  7. $Ghost="ashpool.micro.umn.EDU";            # host THIS daemon is running on
  8.  
  9. #----------------------------------------------------------------------
  10.  
  11. #Hmm.  I'm really not sure how to catch signals...
  12. $SIG{'INT'} = 'Gabort';
  13. $SIG{'HUP'} = 'Gabort';
  14. $SIG{'QUIT'} = 'Gabort';
  15. $SIG{'PIPE'} = 'Gabort';
  16. $SIG{'ALRM'} = 'Gabort';
  17.  
  18.  
  19. &go4gw_main if (!defined($GnotServer));
  20.  
  21. sub go4gw_main {
  22.  
  23. # get command
  24. $_ = <STDIN>; s/\r//; s/\n//;
  25.  
  26. ($gw,$args) = /^(\w+)\s*(.*)$/ if (!/^$/);
  27.  
  28. open(CONF,$Gconf_file) || &Gabort("$Gconf_file: $!");
  29. while(<CONF>) { 
  30.      chop;
  31.      next if /^#/ || /^$/;
  32.      ($gateway,$user,$module,$title) = split(/:/);
  33.      if ($gw eq '' && $title) { &Greply("1$title\t$gateway\t$Ghost\t$Gport"); }
  34.      elsif ($gw eq $gateway) { &launch_gateway($user,$gateway,$module,$args); }
  35. }
  36.  
  37. if ($gw eq '') { &Greply("."); exit; }
  38. else { &Gabort("3No such gateway: $gw"); }
  39.  
  40. }
  41. sub launch_gateway {
  42.    local($user,$gateway,$module,$args) = @_;
  43.  
  44.    if ($user =~ /^\-?\d+$/) { $Guid = $user; }
  45.    elsif ($user eq '') { $Guid = -2; }
  46.    else {           
  47.        ($n,$pw,$Guid) = getpwnam($user);
  48.        if ($Guid eq '') { $Guid = "-2"; }
  49.    }
  50.  
  51.   ($<,$>) = ($Guid,$Guid) unless $>;
  52.    &Gabort("Can't load gateway: $module") if (! -e $module);
  53.    require "$module";
  54.    $main = "${gateway}_main";
  55.    $Ggw = $gateway;
  56.    &$main($args);
  57.    &Greply(".");  # shouldn't really get here, but what the hack!
  58.    exit;
  59. }
  60.  
  61. #
  62. # standard routines
  63. #
  64.  
  65. sub Greply { print "$_[0]\r\n"; }
  66.  
  67. sub Gabort { print "3$_[0]\r\n.\r\n"; exit; }
  68.  
  69. sub GopenServer {
  70.  
  71.  local($server,$port) = @_;
  72.  $sockaddr = 'S n a4 x8';
  73.  (($name, $aliases, $type, $len, $saddr) = gethostbyname($server))
  74.         || &Gabort("3Can't get address of: $server");
  75.  $sin = pack($sockaddr, 2, $port, $saddr);
  76.  socket(GSERVER, 2, 1, 0) || &Gabort("Can't create socket: $!");
  77.  connect(GSERVER, $sin)   || &Gabort("Can't connect to server: $!");
  78.  select(GSERVER); $| = 1; select(STDOUT); $| = 1;
  79. }
  80.  
  81. sub GcloseServer {
  82.   close(GSERVER);
  83. }
  84.  
  85. sub Gsend { 
  86.      print "send -> |$_[0]|\n" if (defined($Gdebug));
  87.      print GSERVER "$_[0]\r\n"; 
  88. }
  89.  
  90. sub Grecv { 
  91.    local ($_); 
  92.    $_= <GSERVER>; 
  93.    s/\n$//;
  94.    s/\r$//;
  95.    print "recv -> |$_|\n" if (defined($Gdebug));
  96.    return $_; 
  97. }
  98.  
  99. sub Gsorry {
  100.  
  101. print<<EOF;
  102.  
  103. Sorry! You have selected information that cannot be delivered off
  104. of campus due to restrictions.
  105.  
  106.    -- The Mole Hole Guardian
  107.  
  108. EOF
  109.  
  110. &Greply(".");
  111. exit;
  112.  
  113. }
  114.  
  115. 1;  # for require
  116.